package com.ssm.helong.shop.service.impl;

import com.ssm.helong.shop.Dao.GoodsAttributeMapper;
import com.ssm.helong.shop.Dao.OrderDetailMapper;
import com.ssm.helong.shop.Dao.OrderMapper;
import com.ssm.helong.shop.Dao.ShoppingCarDao;
import com.ssm.helong.shop.common.OrderStatusEnum;
import com.ssm.helong.shop.common.PayStatusEnum;
import com.ssm.helong.shop.common.PayTypeEnum;
import com.ssm.helong.shop.common.ServiceResultEnum;
import com.ssm.helong.shop.controller.vo.OrderVO;
import com.ssm.helong.shop.entity.Order;
import com.ssm.helong.shop.service.OrderService;
import com.ssm.helong.shop.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private ShoppingCarDao shoppingCarDao;
    @Autowired
    private GoodsAttributeMapper goodsAttributeMapper;

    @Override
    public String updateOrderInfo(Order order) {
        Order temp = orderMapper.selectByPrimaryKey(order.getOrderId());
        //不为空且orderStatus>=0且状态为出库之前可以修改部分信息
        if (temp != null && temp.getOrderStatus() >= 0 && temp.getOrderStatus() < 3) {
            temp.setTotalPrice(order.getTotalPrice());
            temp.setReceivAddress(order.getReceivAddress());
            temp.setUpdateTime(new Date());
            if (orderMapper.updateOrder(temp) > 0) {
                return ServiceResultEnum.SUCCESS.getResult();
            }
            return ServiceResultEnum.DB_ERROR.getResult();
        }
        return ServiceResultEnum.DATA_NOT_EXIST.getResult();
    }

    @Override
    public String checkDone(Integer orderId) {
        Order order = orderMapper.selectByPrimaryKey(orderId);
        if (order != null) {
            if (order.getOrderStatus() != 1) {
                return order.getOrderNo() + "订单的状态不是支付成功或配货完成无法执行出库操作";
            }
            if (order.getOrderStatus() == 1) {
                orderMapper.checkDone(orderId);
                return ServiceResultEnum.SUCCESS.getResult();
            }
        }
        //未查询到数据 返回错误提示
        return ServiceResultEnum.DATA_NOT_EXIST.getResult();
    }

    @Override
    public String checkOut(Integer orderId) {
        Order order = orderMapper.selectByPrimaryKey(orderId);
        if (order != null) {
            if (order.getOrderStatus() != 2) {
                return order.getOrderNo() + "订单的状态不是配货完成无法执行出库操作";
            }
            if (order.getOrderStatus() == 2) {
                orderMapper.checkOut(orderId);
                return ServiceResultEnum.SUCCESS.getResult();
            }
        }
        //未查询到数据 返回错误提示
        return ServiceResultEnum.DATA_NOT_EXIST.getResult();
    }

    @Override
    public String closeOrder(Integer orderId) {
        Order order = orderMapper.selectByPrimaryKey(orderId);
        if (order != null) {
            if (order.getOrderStatus() != 4) {
                return order.getOrderNo() + "订单的状态不是交易成功无法执行出库操作";
            }
            if (order.getOrderStatus() == 4 || order.getOrderStatus() < 0) {
                if (orderMapper.closeOrder(orderId, 3) > 0) {
                    return ServiceResultEnum.SUCCESS.getResult();
                } else {
                    return ServiceResultEnum.DB_ERROR.getResult();
                }
            }

        }
        //未查询到数据 返回错误提示
        return ServiceResultEnum.DATA_NOT_EXIST.getResult();
    }

    @Override
    public OrderVO getOrderDetailByOrderNo(String orderNo, Integer userId) {
        Order order = orderMapper.selectByOrderNo(orderNo);
        if (order != null) {
            OrderVO orderVO = new OrderVO();
            BeanUtil.copyProperties(order, orderVO);
            orderVO.setOrderStatusString(OrderStatusEnum.getOrderStatusEnumByStatus(orderVO.getOrderStatus()).getName());
            orderVO.setPayStatusString(PayTypeEnum.getPayTypeEnumByType(orderVO.getPayType()).getName());
            return orderVO;
        }
        return null;
    }

    @Override
    public Order getOrderByOrderNo(String orderNo) {
        return orderMapper.selectByOrderNo(orderNo);
    }

    @Override
    public String cancelOrder(String orderNo, Integer userId) {
        Order order = orderMapper.selectByOrderNo(orderNo);
        if (order != null) {
            if (orderMapper.closeOrder(order.getOrderId(), OrderStatusEnum.ORDER_CLOSED_BY_MALLUSER.getOrderStatus()) > 0) {
                return ServiceResultEnum.SUCCESS.getResult();
            } else {
                return ServiceResultEnum.DB_ERROR.getResult();
            }
        }
        return ServiceResultEnum.ORDER_NOT_EXIST_ERROR.getResult();
    }

    @Override
    public String finishOrder(String orderNo, Integer userId) {
        Order order = orderMapper.selectByOrderNo(orderNo);
        if (order != null) {
            order.setOrderStatus(OrderStatusEnum.ORDER_SUCCESS.getOrderStatus());
            order.setUpdateTime(new Date());
            if (orderMapper.updateOrder(order) > 0) {
                return ServiceResultEnum.SUCCESS.getResult();
            } else {
                return ServiceResultEnum.DB_ERROR.getResult();
            }
        }
        return ServiceResultEnum.ORDER_NOT_EXIST_ERROR.getResult();
    }

    @Override
    public String paySuccess(String orderNo, Integer payType) {
        Order order = orderMapper.selectByOrderNo(orderNo);
        if (order != null) {
            order.setOrderStatus(OrderStatusEnum.OREDER_PAID.getOrderStatus());
            order.setPayType(payType);
            order.setPayStatus(PayStatusEnum.PAY_SUCCESS.getPayStatus());
            order.setPayTime(new Date());
            order.setUpdateTime(new Date());
            if (orderMapper.updateOrder(order) > 0) {
                return ServiceResultEnum.SUCCESS.getResult();
            } else {
                return ServiceResultEnum.DB_ERROR.getResult();
            }
        }
        return ServiceResultEnum.ORDER_NOT_EXIST_ERROR.getResult();
    }


}
